package com.fbs.wowo.manage.service.imp;

import com.fbs.wowo.exception.DataOverflowException;
import com.fbs.wowo.exception.ErrorCode;
import com.fbs.wowo.exception.NoSuchDataException;
import com.fbs.wowo.manage.entity.Account;
import com.fbs.wowo.manage.entity.AccountDetail;
import com.fbs.wowo.manage.entity.User;
import com.fbs.wowo.manage.mapper.AccountDetailMapper;
import com.fbs.wowo.manage.mapper.AccountMapper;
import com.fbs.wowo.manage.service.AccountDetailService;
import com.fbs.wowo.manage.service.AccountService;
import com.fbs.wowo.manage.service.UserService;
import com.fbs.wowo.util.common.Generator;
import com.fbs.wowo.util.common.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("accountDetailService")
@Transactional
public class AccountDetailServiceImp implements AccountDetailService {
    //状态1          1审核中        2成功

    /**
     * 实名认证
     * 绑定微信
     * 交易密码
     *
     * @param userId
     */
    @Override
    public void certBindWxPayPass(String userId) {
        Validator.verifyStr(userId);
        AccountDetail ad = accountDetailMapper.findByUserId(userId);
        if (ad == null) throw new NoSuchDataException(ErrorCode.ERR_CERT_NULL);
        if (!StringUtils.hasText(ad.getWxOpenId())) throw new NoSuchDataException(ErrorCode.ERR_PAY_WX_BIND);
        Account account = accountMapper.findByUserId(userId);
        if (account == null) throw new NoSuchDataException(ErrorCode.ERR_ACCOUNT_NULL);
        if (!StringUtils.hasText(account.getPassword())) throw new NoSuchDataException(ErrorCode.ERR_PASS_NULL);
    }

    /**
     * 实名认证
     *
     * @param record
     */
    @Override
    public void cert(AccountDetail record) {
        //判断用户是否存在
        User user = userService.findByUserId(record.getUserId());
        if (user == null)
            throw new NoSuchDataException(ErrorCode.ERR_USER_NOT_EXIST);

        //用户ID是否已认证
        AccountDetail adUser = accountDetailMapper.findByUserId(record.getUserId());
        if (adUser != null ){
           if (adUser.getCertStatus()==3){
               adUser.setCompanyName(record.getCompanyName());          //公司名
               adUser.setCompanyCertType(record.getCompanyCertType()); //证件类型
               adUser.setCompanyCertNum(record.getCompanyCertNum());   //证件号
               adUser.setCompanyBlImg(record.getCompanyBlImg());           //证件照
               adUser.setCompanyAttorneyImg(record.getCompanyAttorneyImg());   //委托书
               adUser.setType(record.getType());
               adUser.setName(record.getName());
               adUser.setIdNum(record.getIdNum());
               adUser.setFrontImg(record.getFrontImg());
               adUser.setContraryImg(record.getContraryImg());
               adUser.setHandImg(record.getHandImg());
               adUser.setCreateTime(new Date());       // 创建时间
               adUser.setCertStatus((byte)1);
               accountDetailMapper.updateByPrimaryKeySelective(adUser);
               return;
           }
           throw new NoSuchDataException(ErrorCode.ERR_CER_TOERR_CER_TO);
        }
        // 身份号是否已存在
        AccountDetail adCert = accountDetailMapper.findByIdNumber(record.getIdNum());
        if (adCert != null)
            throw new NoSuchDataException(ErrorCode.ERR_IDNUMBER_TO);
        //修改用户认证状态   1代表审核中
        AccountDetail ad = new AccountDetail();
        ad.setId(Generator.generateId());   // 生成主键
        ad.setUserId(record.getUserId());
        ad.setCreateTime(new Date());       // 创建时间
        ad.setCertStatus((byte) 1);         // 状态1          1审核中        2成功
        ad.setName(record.getName());
        ad.setIdNum(record.getIdNum());
        ad.setCertType(record.getCertType());   // 1 身份证
        ad.setFrontImg(record.getFrontImg());
        ad.setContraryImg(record.getContraryImg());
        ad.setHandImg(record.getHandImg());
        ad.setType(record.getType());
        ad.setCompanyName(record.getCompanyName());     //公司名
        ad.setCompanyCertType(record.getCompanyCertType()); //证件类型
        ad.setCompanyCertNum(record.getCompanyCertNum());   //证件号
        ad.setCompanyBlImg(record.getCompanyBlImg());           //证件照
        ad.setCompanyAttorneyImg(record.getCompanyAttorneyImg());   //委托书
        accountDetailMapper.insertSelective(ad);
    }

    /**
     * 绑定银行卡
     *
     * @param record
     */
    @Override
    public void bankBinding(AccountDetail record) throws NoSuchDataException {
        String userId = record.getUserId();
        AccountDetail ad = new AccountDetail();
        ad.setUserId(record.getUserId());
        ad.setBankNum(record.getBankNum());
        ad.setBankCode(record.getBankCode());
        ad.setTel(record.getTel());

        AccountDetail adDb = accountDetailMapper.findByUserId(userId);
        if (adDb == null) {
            throw new NoSuchDataException("需要先实名认证");
        }
        ad.setId(adDb.getId());
        accountDetailMapper.updateByPrimaryKeySelective(ad);
    }

    /**
     * 查找用户账户信息
     *
     * @param userId
     * @return
     */
    @Override
    public AccountDetail findByUserId(String userId) {
        return accountDetailMapper.findByUserId(userId);
    }

    /**
     * 修改账户信息
     *
     * @param record
     */
    @Override
    public void modify(AccountDetail record) {
        accountDetailMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<AccountDetail> findInReviewList(String type) {
        return accountDetailMapper.findInReviewList(type);
    }


    /**
     * 审核       成功传2,失败传3
     */
    public int updateAuditState(String userId,int state){
        AccountDetail ad = accountDetailMapper.findByUserId(userId);
        if (ad!=null){
            if (ad.getCertStatus()==1){
                ad.setCertStatus((byte)state);
                return accountDetailMapper.updateByPrimaryKeySelective(ad);
            }
        }
        throw new DataOverflowException(ErrorCode.ERR_CER_NULL);
    }

    @Resource
    private AccountDetailMapper accountDetailMapper;
    @Resource
    private AccountMapper accountMapper;
    @Autowired
    private AccountService accountService;
    @Resource
    private UserService userService;
}
